#!/usr/bin/env python

SLOPE_INITIAL_VALUE = 1000

from backup import add_subelement
from xml.etree.ElementTree import SubElement

class Slope(object):
	"""Object which represent a slope"""
	def __init__(self, id, initial_value=SLOPE_INITIAL_VALUE, name=None):
		super(Slope, self).__init__()
#		self.name = name
		self.id = id
		self.initial_value = initial_value
		self.successfull_climbings = 0
	
	def set_initial_value(self, value):
		self.initial_value = value
	
	def raise_climbings(self):
		"""
		>>> slope = Slope(0)
		>>> a = slope.successfull_climbings
		>>> slope.raise_climbings()
		>>> slope.successfull_climbings - a
		1
		"""
		self.successfull_climbings += 1

	def drop_climbings(self):
		"""
		>>> slope = Slope(0)
		>>> slope.raise_climbings()
		>>> a = slope.successfull_climbings
		>>> slope.drop_climbings()
		>>> slope.successfull_climbings - a
		-1
		"""
		self.successfull_climbings -= 1
		self.successfull_climbings = max(0, self.successfull_climbings)
	
	def reset_climbings(self):
		"""
		>>> slope = Slope(0)
		>>> a = slope.successfull_climbings
		>>> slope.raise_climbings()
		>>> slope.successfull_climbings
		1
		>>> slope.reset_climbings()
		>>> slope.successfull_climbings
		0
		"""
		self.successfull_climbings = 0
	
	def get_value(self):
		"""
		>>> slope = Slope(0)
		>>> slope.raise_climbings()
		>>> slope.raise_climbings()
		>>> slope.get_value() - SLOPE_INITIAL_VALUE/2
		0
		"""
		return int(self.initial_value/self.successfull_climbings)

	def _add_node(self, xml_node):
		slope_node = add_subelement(xml_node, 'slope', None, id=str(self.id))
#		name_node = add_subelement(slope_node, 'name', self.name)
		initialvalue_node = add_subelement(slope_node, 'initialvalue', self.initial_value)
		successfull_node = add_subelement(slope_node, 'successfullclimbings', self.successfull_climbings)

	def save(self, xml_node):
		self._add_node(xml_node)
